import { _decorator, Component, Node, Prefab, Vec3 } from 'cc';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import { GameApp } from '../../GameApp';
import { enemyBase } from '../enemyBase';
import { boss_aoe } from '../enemySkill/boss_aoe';
import { boss_arrow } from '../enemySkill/boss_arrow';

const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('magician')
export class magician extends enemyBase {


    @property({ type: Prefab, displayName: "狂暴技能预制体" })
    protected skillPrefab2: Prefab = null


    @property({ type: Prefab, displayName: "普通技能预制体" })
    protected skillPrefab1: Prefab = null
    @property({ type: Prefab, displayName: "狂暴技能预警" })
    protected skillWaming: Prefab = null

    @property({ type: Number, displayName: "狂暴百分比" })
    protected kuangbao: number = 0.5

 

    private nowSwaming: Node = null



    private atime:number=3
    private nowTime:number=0

    private isA: boolean = false//怪物正在攻击

    onLoad(){
        super.onLoad()
    }

    start() {

    }
    friendly_pursuit() {
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
       super.friendly_pursuit()
    }
    pursuit_building() {
     //   this.unschedule(this.fire.bind(this))
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.pursuit_building()
    }

    enemy_pursuit() {
      //  this.unschedule(this.fire.bind(this))
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.enemy_pursuit()
    }
    hit(num: number,how:Node) {
        super.hit(num,how)
        if(this.hp/this.totalHP<=this.kuangbao){
             this.isKuangbao=true
        }




    }


    idle() {
       // this.unschedule(this.fire.bind(this))
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.idle()

    }
    attack() {
        super.attack()
          
            
        if (this.isKuangbao) {
    


          //  this.NavTs.StopNav()
            this.node.lookAt(this.attackTarget.getWorldPosition())
            this.enemyModel.playAni(this.attackAni1, true)


          //  this.nowSwaming.lookAt(this.attackTarget.worldPosition)
            //this.scheduleOnce(this.fire.bind(this), 2)
        } else {

     
        if (this.isA) {
               

            return
        }
   
        this.node.lookAt(this.attackTarget.getWorldPosition())

      //  this.unschedule(this.fire.bind(this))
        this.isA = true


        if (!this.nowSwaming) {
            this.nowSwaming = poolManager.instance.getNode(this.skillWaming, GameApp.Instance.wrramingRoot)


        }
   
        
        this.nowSwaming.active = true
        this.nowSwaming.setWorldPosition(this.attackTarget.worldPosition.x,1,this.attackTarget.worldPosition.z)







        }


    }
    die(){
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.die()
    }
    attickCallback() {
     
        if (this.isKuangbao) {    
            const k2: Node = poolManager.instance.getNode(this.skillPrefab2, GameApp.Instance.skillRoot)
            k2.setWorldPosition(this.node.worldPosition.x,3,this.node.worldPosition.z)

            k2.forward=this.node.forward


            k2.children.forEach((ndArrowItem: Node) => {
                let scriptArrowItem: boss_arrow = null
                scriptArrowItem = ndArrowItem.getComponent(boss_arrow) as boss_arrow;
                scriptArrowItem.init(this.node,15, this.node.getWorldPosition(), false, this.node.worldPosition);


            })
            
            //近战攻击玩家正常掉血
            





      


      
      
        } else {
            if(!this.nowSwaming){
                return
            }
            const k: Node = poolManager.instance.getNode(this.skillPrefab1, GameApp.Instance.skillRoot)
            k.setWorldPosition(0, 20, 0)
      
            const ts:boss_aoe=k.getComponent(boss_aoe)
            if(ts){
               ts.init(this.nowSwaming.worldPosition,this.node,20,()=>{
                this.nowSwaming.active = false
                this.isA = false
               })
            }
        }

    }//攻击回调

    // private fire() {
    //     console.log("isfire");
        
    //     this.NavTs.StopNav()
    //     this.enemyModel.playAni(this.attackAni1, false,()=>{
    //         this.enemyModel.playAni(this.idelAni, true)
    //     })
    // }
    update(dt: number) {
        if(this.isA){
            this.nowTime+=dt
            if(this.nowTime>=this.atime){
                this.nowTime=0
            
                this.NavTs.StopNav()
                this.enemyModel.playAni(this.attackAni1, false,()=>{
                    this.isA=false
                    this.NavTs.StopNav()
                    this.enemyModel.playAni(this.idelAni, true)
                })
            }
        }

        super.update(dt)
    }
}

